// You are given two non-empty linked lists representing two non-negative integers.
// The digits are stored in reverse order and each of their nodes contain a single digit.
// Add the two numbers and return it as a linked list.
// You may assume the two numbers do not contain any leading zero, except the number 0 itself.
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8

//  模拟加法运算

/**
 * 模拟单向链表
 * @param {[numbers]} val [值]
 */
function ListNode(val) {
  this.val = val;
  this.next = null; //下一个节点
}

/**
 * 两个链表相加成为新链表
 * @param {[ListNode]} listNode1 [第一个链表的第一个节点]
 * @param {[ListNode]} listNode2 [第二个链表的第一个节点]
 */
function addTwoNumbers(listNode1, listNode2) {
  var firstNode = new ListNode(0);
  var curNode = firstNode;
  var sum = 0;
  while (true) {
    if (listNode1 !== null) {
      sum += listNode.val;
      listNode1 = listNode1.next;
    }
    if (listNode2 !== null) {
      sum += listNode2.val;
      listNode2 = listNode2.next;
    }
    curNode.val = sum % 10;
    sum /= 10;
    if (listNode1 !== null || listNode2 !== null || sum !== 0) {
      curNode = (curNode.next = new ListNode(0));
    } else {
      break;
    }
  }
  return firstNode;
}
